﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class ReportDAO
    {
        TestOnlineDBEntities db = new TestOnlineDBEntities();

        public List<Report> Select()
        {
            try
            {
                var reports = db.Reports.ToList();
                return reports;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Report SelectByReportId(int reportId)
        {
            try
            {
                var report = db.Reports.Where(r => r.ReportId == reportId).FirstOrDefault();
                return report;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Select list Report by DiscussionId
        /// </summary>
        /// <param name="discussionId">Id of Discussion</param>
        /// <returns>List Reports</returns>

        public List<Report> SelectByDiscussionId(int discussionId)
        {
            var reports = db.Reports.Where(x => x.DiscussionId == discussionId).ToList();
            return reports;
        }

        public int Insert(Report report)
        {
            try
            {
                db.Reports.AddObject(report);
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(Report report)
        {
            try
            {
                var rep = db.Reports.Where(r => r.ReportId == report.ReportId).FirstOrDefault();
                rep.IsChecked = report.IsChecked;
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Delete(int reportId)
        {
            try
            {
                var report = db.Reports.Where(r => r.ReportId == reportId).FirstOrDefault();
                db.Reports.DeleteObject(report);
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int DeleteByUserId(int userId)
        {
            try
            {
                var reports = db.Reports.Where(r => r.UserId == userId).ToList();
                foreach (var r in reports)
                {
                    db.Reports.DeleteObject(r);
                }
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

    }
}